package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Setmeal;
import cn.itcast.pojo.Vo.OrderVo;
import cn.itcast.service.OrderService;
import cn.itcast.service.SetmealService;
import cn.itcast.utils.DateUtils;
import cn.itcast.vo.OrderVos;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private SetmealService setmealService;

    /**
     * 预约提交
     * @param member
     * @return
     */

    @Override
    public Result orderSubmit(Map map,Member member) {
        //构建Order对象保存到数据库中
        Order order = new Order();
        String uuId = UUID.randomUUID().toString().replaceAll("-", "");
        order.setId(uuId);

        order.setMemberId(member.getId()); //会员id

        String name = (String) map.get("name");
        order.setName(name); //姓名

        String orderDate = (String) map.get("orderDate"); //创建时间
        Date date = null;
        try {
            date = DateUtils.parseString2Date(orderDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        order.setOrderDate(date); //预约日期
        order.setOrderType(Order.ORDERTYPE_WEIXIN); //预约方式
        order.setOrderStatus(1); //预约状态

        String setmealId = (String) map.get("setmealId");
        Integer stID = Integer.valueOf(setmealId);
        order.setSetmealId(stID); //体检套餐id

        order.setArrival(0); //是否到诊


        String idCard = (String) map.get("idCard");
        order.setIdCard(idCard); //身份证号

        String sex = (String) map.get("sex"); //性别
        order.setSex(sex);

        Integer addressId = (Integer) map.get("addressId"); //分院地址id
        order.setAddressId(addressId);

        String phoneNumber = (String) map.get("phoneNumber"); //电话号码
        order.setPhoneNumber(phoneNumber);

        order.setPhoneNumber("0");  //支付状态

        //如果当前用户已经预约过该体检套餐, 不允许预约  (身份证相同即为同一用户)
        //根据身份号和套餐Id查询,如果已经存在改订单,抛出异常
        if (orderDao.findOderByIdCard(idCard,setmealId)!=null){
            return new Result(false,"不可重复预约",null);
        }
        //如果选择的日期没有设置过预约, 不允许预约
        //根据日期查询当日是否可以预约
        Integer num = orderDao.findByDate(date);
        if (num<=0){
            return new Result(false,"该日期没有开启预约,请重新选择",null);
        }
        //如果选择的日期已经约满, 不允许预约
        //根据日期查询出当天的一预约人数
        Integer reservationsNum =   orderDao.findByReservations(date);
        if (num-reservationsNum<=0){
            return new Result(false,"当前日期预约已满,请选择其他日期",null);
        }
        //调用Dao保存
        Integer result = orderDao.save(order);
        if (result<1){
            return new Result(false,"预约失败",null);
        }
        //预约成功后, 扣减可预约人数
        orderDao.updateReservations(date);
        //构建Member 保存到表中
        Member newMember = new Member();
        newMember.setId(member.getId());
        newMember.setName(name);
        newMember.setIdCard(idCard);
        newMember.setSex(sex);
        newMember.setPhoneNumber(phoneNumber);
        memberDao.update(member);

        return new Result(true,"预约成功",uuId);
    }

    /**
     * 订单回显
     * @return
     */
    @Override
    public OrderVos findByOrderId(String id) {
        //根据id查询出订单详情
        Order order = orderDao.findByOrderId(id);
        //获取体检套餐id
        Integer setmealId = order.getSetmealId();
        //根据套餐id查询出member详情
        Setmeal setmeal = setmealService.findById(setmealId);

        Map setMap=new HashMap();
        setMap.put("name",setmeal.getName());
        //获取分院地址id
        Integer addressId = order.getAddressId();
        //根据id查询分院详情
        Address address = setmealService.findByAddressId(addressId);
        Map map=new HashMap();
        map.put("name",address.getName());


        Date date = order.getOrderDate();
        String orderDate=null;
        try {
            orderDate = DateUtils.parseDate2String(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //构建vo
        OrderVos vo= new OrderVos();
        vo.setId(id); //订单id
        vo.setName(order.getName()); //姓名
        vo.setOrderDate(orderDate); //体检时间
        vo.setOrderType(order.getOrderType());//预约类型
        vo.setSetmeal(setMap);//体检套餐
        //vo.setSetmealName(member.getName());//体检套餐
        //vo.setAddressName(address.getName()); //预约分院
        vo.setAddress(map);
        return vo;
    }

    @Override
    public void updatePayStatus(String id) {

    }


    //分页条件查询
    @Override
    public Result findPage(Map map) {
        Integer page = Integer.parseInt(map.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        String phoneNumber = "";
        if (map.get("queryString") != null){
            phoneNumber = map.get("queryString").toString();
        }
        String startDate ="";
        if (map.get("minTime") != null){
            startDate = map.get("minTime").toString();
        }
        String endDate ="";
        if (map.get("maxTime") != null){
            endDate = map.get("maxTime").toString();
        }
        String orderType ="";
        if (map.get("orderType") != null){
            orderType = map.get("orderType").toString();
        }
        Integer payStatus = null;
        if (map.get("payStatus") !=null){
            payStatus = Integer.parseInt(map.get("payStatus").toString());
        }

        PageHelper.startPage(page,pageSize);

        //查询数据
        List<cn.itcast.pojo.Vo.OrderVo> list = orderDao.findPage(phoneNumber,startDate,endDate,orderType,payStatus);

        PageInfo<OrderVo> info = new PageInfo(list);

        PageResult pageResult = new PageResult(info.getTotal(),info.getList());

        if (info == null){
            return new Result(false,"查询失败",pageResult);
        }else{
            return new Result(true,null,pageResult);
        }
    }


    //通过id的值去判断是   添加预约信息  还是  修改
    @Override
    public Integer insert(Order order) {
        Integer i=0;
        if (order.getId()==null){
            //设置随机的id
            String s = UUID.randomUUID().toString();
            order.setId(s);
            i= orderDao.insert(order);
        }else{
            i = orderDao.update(order);
        }
        return i;
    }

    //通过id查询ordervo对象
    @Override
    public Result findById(String id) {
        cn.itcast.pojo.Vo.OrderVo orderVo =  orderDao.findById(id);

        return orderVo!=null? new Result(true,"数据显示成功",orderVo):new Result(false,"数据显示失败",null);
    }

    /**
     * 编辑就诊状态
     * @param id
     * @param arrival
     * @return
     */
    @Override
    public Result updateArrival(String id, Integer arrival) {
        Integer i = orderDao.updateArrival(id,arrival);
        return i>0? new Result(true,"编辑就诊状态成功"):new Result(false,"编辑就诊状态失败");
    }

}
